# Gateway路由测试脚本 (PowerShell版本)
# 用于验证crossborder模块的路由配置是否正确

$GatewayUrl = "http://localhost:48080"

Write-Host "=== Gateway路由配置测试 ===" -ForegroundColor Cyan
Write-Host "Gateway地址: $GatewayUrl" -ForegroundColor White
Write-Host ""

# 测试函数
function Test-Route {
    param(
        [string]$Url,
        [string]$ExpectedService,
        [string]$Description
    )
    
    Write-Host "测试: $Description" -ForegroundColor Yellow
    Write-Host "URL: $Url" -ForegroundColor Gray
    Write-Host "期望路由到: $ExpectedService" -ForegroundColor Gray
    
    try {
        # 发送请求并获取响应
        $response = Invoke-WebRequest -Uri $Url -Method GET -TimeoutSec 10 -ErrorAction SilentlyContinue
        $statusCode = $response.StatusCode
        
        if ($statusCode -eq 200) {
            Write-Host "✅ 状态码: $statusCode (成功)" -ForegroundColor Green
        } elseif ($statusCode -eq 401 -or $statusCode -eq 403) {
            Write-Host "⚠️  状态码: $statusCode (需要认证，但路由正常)" -ForegroundColor Yellow
        } else {
            Write-Host "⚠️  状态码: $statusCode (其他状态)" -ForegroundColor Yellow
        }
    }
    catch {
        $statusCode = $_.Exception.Response.StatusCode.value__
        if ($statusCode -eq 404) {
            Write-Host "❌ 状态码: $statusCode (路由失败或服务未启动)" -ForegroundColor Red
        } elseif ($statusCode -eq 401 -or $statusCode -eq 403) {
            Write-Host "⚠️  状态码: $statusCode (需要认证，但路由正常)" -ForegroundColor Yellow
        } else {
            Write-Host "❌ 请求失败: $($_.Exception.Message)" -ForegroundColor Red
        }
    }
    Write-Host "---" -ForegroundColor Gray
}

Write-Host "开始测试crossborder模块路由..." -ForegroundColor Cyan
Write-Host ""

# 测试订单相关接口 -> crossborder-order-server
Test-Route -Url "$GatewayUrl/admin-api/crossborder/order-item/page?pageNo=1&pageSize=10" `
           -ExpectedService "crossborder-order-server" `
           -Description "订单明细分页接口"

Test-Route -Url "$GatewayUrl/admin-api/crossborder/order/list" `
           -ExpectedService "crossborder-order-server" `
           -Description "订单列表接口"

Test-Route -Url "$GatewayUrl/admin-api/crossborder/order-shipment/list" `
           -ExpectedService "crossborder-order-server" `
           -Description "发货单列表接口"

# 测试商品相关接口 -> crossborder-product-server
Test-Route -Url "$GatewayUrl/admin-api/crossborder/product/list" `
           -ExpectedService "crossborder-product-server" `
           -Description "商品列表接口"

Test-Route -Url "$GatewayUrl/admin-api/crossborder/category/list" `
           -ExpectedService "crossborder-product-server" `
           -Description "商品分类接口"

# 测试库存相关接口 -> crossborder-inventory-server
Test-Route -Url "$GatewayUrl/admin-api/crossborder/inventory/list" `
           -ExpectedService "crossborder-inventory-server" `
           -Description "库存列表接口"

Test-Route -Url "$GatewayUrl/admin-api/crossborder/warehouse/list" `
           -ExpectedService "crossborder-inventory-server" `
           -Description "仓库列表接口"

# 测试物流相关接口 -> crossborder-logistics-server
Test-Route -Url "$GatewayUrl/admin-api/crossborder/logistics/list" `
           -ExpectedService "crossborder-logistics-server" `
           -Description "物流列表接口"

# 测试财务相关接口 -> crossborder-finance-server
Test-Route -Url "$GatewayUrl/admin-api/crossborder/finance/list" `
           -ExpectedService "crossborder-finance-server" `
           -Description "财务列表接口"

# 测试平台对接接口 -> crossborder-platform-server (兜底路由)
Test-Route -Url "$GatewayUrl/admin-api/crossborder/platform/health" `
           -ExpectedService "crossborder-platform-server" `
           -Description "平台健康检查接口"

Test-Route -Url "$GatewayUrl/admin-api/crossborder/webhook/test" `
           -ExpectedService "crossborder-platform-server" `
           -Description "Webhook测试接口"

Write-Host ""
Write-Host "=== 测试完成 ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "说明：" -ForegroundColor White
Write-Host "✅ 200状态码表示路由成功且接口正常" -ForegroundColor Green
Write-Host "⚠️  401/403状态码表示路由成功但需要认证" -ForegroundColor Yellow
Write-Host "❌ 404状态码表示路由失败或目标服务未启动" -ForegroundColor Red
Write-Host ""
Write-Host "如果看到404错误，请检查：" -ForegroundColor White
Write-Host "1. 对应的微服务是否已启动" -ForegroundColor Gray
Write-Host "2. 服务是否已注册到Nacos" -ForegroundColor Gray
Write-Host "3. Gateway配置是否正确" -ForegroundColor Gray
Write-Host "4. 接口路径是否存在" -ForegroundColor Gray
